package com.cjzuji.rental.dao.impl;

import com.cjzuji.rental.dao.XWuBillDao;
import com.cjzuji.rental.mapper.OrderBillMapper;
import com.cjzuji.rental.pojo.OrderBill;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class XWuBillDaoImpl implements XWuBillDao {

    private static final Logger logger = LoggerFactory.getLogger(XWuBillDaoImpl.class);
    @Autowired
    OrderBillMapper orderBillMapper;
    @Override
    @Transactional
    public  void save(Map<String,Object> data, Long orderID){
        List<Map<String, Object>> orderBilllist = (List<Map<String, Object>>) data.get("orderByStagesDtoList");
        List<OrderBill>  billList = new ArrayList<>();
        for (Map<String, Object> item : orderBilllist){
            //还款状态
            String repayStatusStr = "未支付";
            if ("2".equals(item.get("status").toString())){
                repayStatusStr = "已支付";
            }
            //实际支付金额
            Double realPayCapital = 0.0;
            if ("已支付".equals(repayStatusStr)){
                realPayCapital = Double.parseDouble(item.get("currentPeriodsRent").toString());
            }
            String realRepayTime = item.get("repaymentDate")!=null ? item.get("repaymentDate").toString() : "";
            String realPayTradeId = item.get("tradeNo")!=null ? item.get("tradeNo").toString() : "";
            OrderBill orderBill = OrderBill.builder()
                    .orderId(orderID)
                    .ordersId(item.get("orderId").toString())
                    .realPayTime(realRepayTime) // 支付时间
                    .term((int) item.get("currentPeriods"))   //租期
                    .realPayCapital(realPayCapital)  //实付金额  actRepayCapital
                    .capital(Double.parseDouble(item.get("currentPeriodsRent").toString()))      //应还金额  currentPeriodsRent
                    .monthRepayment(Double.parseDouble(item.get("currentPeriodsRent").toString()))  //月租金  currentPeriodsRent
                    //.zfbDiscountAmt("")  //支付宝优惠金额
                    //.ovdueBuyoutAmt((Double) item.get("termSurplusBuyoutAmt"))     //逾期买断金
                    //.writeOffAmt((Double) item.get("discountAmt"))      //优惠金额
                    //.reduceAmt((Double) item.get("reduceAmt"))      //减免金额
                    //.overdueAmount((Double) item.get("realOverdueFine"))       //逾期违约金
                    .repayDateFmt(item.get("statementDate").toString())  //还款日
                    .realPayTradeId(realPayTradeId) // 商家交易号
                    .repayStatusStr(repayStatusStr)  //还款状态
                    .realRepayTime(realRepayTime)   //还款时间
                    .createTime(new Date())
                    .updateTime(new Date())
                    .build();
            billList.add(orderBill);
        }

        try {
            // System.out.println("批量保存：~~~~~~~~~~"+ billList.toString());
            orderBillMapper.insertBatchSomeColumn(billList);
        }catch (Exception e){
            logger.info("账单信息添加错误" + e.getMessage());
            throw e; // 重新抛出，确保事务回滚
        }
    }
    @Override
    public List<OrderBill> getOrderBillList(Long orderId) {
        return orderBillMapper.getOrderBillList(orderId);
    }
}
